package tcpdemo.tcptest2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Server {
    public static void main(String[] args) throws IOException {
        // 创建服务器
        ServerSocket ss = new ServerSocket(10000);
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                5,// 核心线程的数量
                10,// 线程池中的线程总数量
                60,// 临时线程的空闲时间
                TimeUnit.SECONDS,// 临时线程的空闲时间的单位
                new ArrayBlockingQueue<>(5),// 阻塞队列
                Executors.defaultThreadFactory(),// 创建线程的方式
                new ThreadPoolExecutor.AbortPolicy()// 任务拒绝策略
                );
        // 等待连接
        while (true){
            Socket accept = ss.accept();
            ThreadSocket ts = new ThreadSocket(accept);
            // new Thread(ts).start();
            pool.submit(ts);
        }
        //ss.close();

    }
}
